/**
 * 游戏列表菜单的管理
 */
$(function() {
    draw_game_menus();

    // 游戏名称
    var curr = urlInfo.params.game || 'bjpk10';
    $("#title span").html(get_game_display_name(curr));
    $("title").html(get_game_display_name(curr));

    $("#edit-menu .ok").click(function() {
        var arr = $("#edit-menu .c3 input");
        var r = {};
        for (var i = 0; i < arr.length; i++) {
            var item = $(arr[i]);
            r[item.attr("game")] = parseInt(item.val());
        }

        context.user.user.game_list = JSON.stringify(r);
        draw_game_menus();
        $("#edit-menu").modal('close');

        var info = {
            game_list: context.user.user.game_list,
        };
        CMD.modify_user_info(null, info, function(r) {
            console.log("修改成功。", r);
        });
    });

    // 玩家登录成功后，需要重绘制下菜单
    var timer = setInterval(function() {
        if (!context.user || !context.user.user) return;
        clearInterval(timer);

        draw_game_menus();
    }, 1000);
});

// 绘制菜单
function draw_game_menus() {
    var c = $(".game_list ul.am-nav");
    var curr = urlInfo.params.game || 'bjpk10';
    var list = get_game_lists();
    var count = 10;
    var menuTemplate = '<li class="__class__ menu" value="__value__"><a href="#">__name__</a></li>';

    // 清理下数据
    $(".game_list .menu-item").remove();
    var ul = $('.game_list .am-dropdown-content');

    for (var i = 0; i < count; i++) {
        if (!list[i]) continue;
        var item = menuTemplate.replace(/__value__/, list[i].game);
        item = item.replace(/__name__/, get_game_display_name(list[i].game));
        if (list[i].game == curr)
            item = item.replace(/__class__/, "am-active menu-item");
        else
            item = item.replace(/__class__/, "menu-item");
        $(item).insertBefore($(".game_list .am-dropdown"));
    }

    // 更多游戏
    for (var i = count; i < list.length; i++) {
        var item = menuTemplate.replace(/__value__/, list[i].game);
        item = item.replace(/__name__/, get_game_display_name(list[i].game));
        item = item.replace(/__class__/, "menu-item");
        ul.append(item);
    }
    {
        var item = menuTemplate.replace(/__value__/, 'edit');
        item = item.replace(/__name__/, '自定义菜单');
        item = item.replace(/__class__/, "menu-item");
        ul.append(item);
    }

    var cb = function(e) {
        var target = $(e.currentTarget);
        var game = target.attr("value");
        if (game == curr) return;
        if (game == 'edit') {
            show_edit_menu_window();
            return;
        }

        // 切换游戏
        Util.jumpUrl({
            game: game,
        });
    };
    var arr = $(".game_list li.menu");
    for (var i = 0; i < arr.length; i++) {
        $(arr[i]).click(cb);
    }
}

// 取得我的菜单配置信息
var menuOrders = {};
function get_game_lists() {
    var list = []; 
    if (window.ME) list = ME.gameLists();
    var games = get_game_list();
    var r = [];
    for (var i in games) {
        var game = games[i];
        if (menuOrders[game] === undefined) continue;
        if (list[game]) 
            r.push({
                game: game,
                order: list[game],
            });
        else
            r.push({
                game: game,
                order: parseInt(i),
            });
    }
    r.sort(function(a, b) {
        var aorder = a.order, border = b.order;
        if (menuOrders[a.game] !== undefined) aorder = parseInt(menuOrders[a.game]);
        if (menuOrders[b.game] !== undefined) border = parseInt(menuOrders[b.game]);
        return aorder - border;
    });
    return r;
}

function show_edit_menu_window() {
    var table = $("#edit-menu tbody");
    table.empty();
    var r = get_game_lists();
    var t = '<tr><td class="c1">__name__</td><td class="c3"><input game="__k__" type="number" value="__v__"/></td></tr>';
    for (var i in r) {
        var menu = r[i];
        var l = t.replace(/__name__/g, get_game_display_name(menu.game));
        l = l.replace(/__k__/g, menu.game);
        l = l.replace(/__v__/g, menu.order);
        table.append(l);
    }

    $("#edit-menu").modal({
        width: 400
    });
}

// 拉取服务器的游戏顺序配置
Classify.queryExt("games", "order", true, {}, function(r) {
    menuOrders = {};
    for (var i in r) {
        if (r[i] == -1) continue;
        menuOrders[i] = r[i];
    }
    draw_game_menus();
});
